import {getQuestionsData,addQuestionData,updateQuestionData,delQuestionData,randomQuestionsData} from '../utils/axios/questions.js'
export const state=()=>{ 
    return{
        questions:[],
        disabledForm:"",
        randomQuestion:[],
        index:0,
    }
}
export const mutations={
    SET_QUESTIONS(state,data){
        state.questions=data
    },
    SET_STATUS(state,payload){
        console.log(payload);
        state.disabledForm=payload
    },
    SET_RANDOM_QUESTIONS(state,data){
        state.randomQuestion=data
    },
    SET_INDEX(state,index){
        state.index=index
    }

   
}
export const actions={
    // 随机获取20道题
    async randomQuestionDataFn({commit}){
        let {data}=await randomQuestionsData()
        commit("SET_RANDOM_QUESTIONS",data.data);
    },
    // 渲染
    async randerQuestionTable({commit}){
        let {data}=await getQuestionsData()
        console.log(data.data);
        commit("SET_QUESTIONS",data.data)
    },
    // 新增
    async handeladdQuestionData({dispatch},form){
        console.log(form);
        let r=await addQuestionData({...form,uid:1,answers:form.answers.sort().join(","),created:  Date.now()})
        console.log(r); 
        dispatch("randerQuestionTable")
    },
    // 删除
    async handeldelQuestionData({dispatch},data){
        await delQuestionData(data)
        dispatch("randerQuestionTable")

    },
    // 修改
    async handelupdateQuestionData({dispatch},form){
        await    updateQuestionData({...form,uid:1,answers:form.answers.sort().join(",")});
        dispatch("randerQuestionTable")
    }
}
export const getters={ 
    getterCurrentQuestion(state){
        // console.log(state.randomQuestion[state.index]);
      return state.randomQuestion[state.index];
    }
}